<script setup lang="ts">
import { onLaunch } from '@dcloudio/uni-app';
import { setStorage, getStorage } from '@/utils/common';
import { memberHeartbeat } from '@/api/login';
import { useMemberStore } from '@/stores/modules/member';
import { useI18n } from 'vue-i18n';

const { locale } = useI18n();
const memberStore = useMemberStore();

onLaunch(() => {
    checkHeartbeat();
    const lang = getStorage('lang') || 'en';
    locale.value = lang;
    setStorage('lang', lang);
    memberStore.setDataInfo({
        lang
    });
    console.log(memberStore.dataInfo);
});

const checkHeartbeat = async () => {
    const token: string = getStorage('token') || '';
    // 检查token是否存在
    if (!token) {
        setTimeout(() => {
            checkHeartbeat();
        }, 10000);
        return;
    }
    setInterval(
        () => {
            getMemberHeartbeat();
        },
        1000 * 60 * 10
    );
};

// 会员心跳
const getMemberHeartbeat = async () => {
    await memberHeartbeat({});
};
</script>

<style lang="scss">
@use '@/styles/index.scss';
</style>
